/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package botlab.control;

import botlab.Game;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;

/**
 *
 * @author Quentin
 */
public class ShooterControl extends BaseControl
{
    private Sphere sphere;
    private RigidBodyControl bulletRBC;
    private Node rootNode = Game.getApp().getRootNode();
    private BulletAppState bulletAppState = Game.getApp().getStateManager().getState(
            BulletAppState.class);
    private static int i = 0;

    public ShooterControl()
    {
        // Initialize the cannon ball geometry    
        sphere = new Sphere(5, 5, 0.1f, true, false);
        sphere.setTextureMode(TextureMode.Projected);
    }

    @Override
    protected void controlUpdate(float tpf)
    {
        if (i > 8)
        {
            newBullet();
            i = 0;
        } else
        {
            i++;
        }
    }

    /**
     * Create a new bullet that will be fired by the spatial in front of it
     */
    public void newBullet()
    {
        Geometry ball_geo = new Geometry("bullet", sphere);
        ball_geo.setMaterial(new Material(Game.getApp().getAssetManager(),
                "Common/MatDefs/Misc/Unshaded.j3md"));

        //avoid an apparition of the object in the middle of the scene
        ball_geo.setLocalTranslation(spatial.getLocalTranslation());
        rootNode.attachChild(ball_geo);

        bulletRBC = new RigidBodyControl(1f);

        //do not interact with others physics objects
        bulletRBC.setCollideWithGroups(
                PhysicsCollisionObject.COLLISION_GROUP_NONE);
        //others physics objects do not interact with it
        bulletRBC.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_NONE);

        //for collision detection
        GhostControl bulletGC=new GhostControl(new BoxCollisionShape(
                new Vector3f(1.f,1.f,1.f)));
        
        ball_geo.addControl(bulletGC);
        ball_geo.addControl(bulletRBC);
        
        bulletAppState.getPhysicsSpace().add(bulletRBC);
        bulletAppState.getPhysicsSpace().add(bulletGC);
        

        bulletRBC.setPhysicsLocation(spatial.getLocalTranslation());
        bulletRBC.setLinearVelocity(spatial.getLocalRotation().mult(Vector3f.UNIT_Z.mult(
                35)));
    }

    @Override
    public Control cloneForSpatial(Spatial spatial)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
